package com.example.administrator.liveapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.MainActivity;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.LoginBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.MD5Utils;
import com.example.administrator.liveapp.utils.SharedPrefsUtil;
import com.google.gson.Gson;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 以前手机号验证码登录activity(已经没有用了)
 */

public class LoginPage2 extends Activity implements View.OnClickListener {

    private TextView registered;//注册入口
    private TextView forget_password;//忘记密码入口
    private Button login_button;
    private EditText login_username;
    private EditText login_password;
    private SharedPrefsUtil sp;
    private String username;//用户名
    private String userpassword;//密码
    private ImageView phone_login;
    private LoginBean bean;
    public ApplicationUtils applicationUtils;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_page);
        applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        listener();
    }


    private void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        registered = (TextView) findViewById(R.id.registered);
        forget_password = (TextView) findViewById(R.id.forget_password);
        login_button = (Button) findViewById(R.id.login_button);
        login_username = (EditText) findViewById(R.id.login_username);
        login_password = (EditText) findViewById(R.id.login_password);
        phone_login = (ImageView) findViewById(R.id.phone_login);
        findViewById(R.id.qq).setOnClickListener(this);
        findViewById(R.id.weixin).setOnClickListener(this);
    }

    private void listener() {
        registered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginPage2.this, RegisteredPage.class);
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });

        forget_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginPage2.this, ForgotPasswordPage.class);
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });

        phone_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginPage2.this, WxLoginPage.class);
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sp = new SharedPrefsUtil();
                username = login_username.getText().toString();
                userpassword = MD5Utils.getMd5Value(login_password.getText().toString());
                String url = AppUrl.URL + AppUrl.LoginURL;
                Map<String, String> map = new HashMap<>();
                map.put("consumerTel", username);
                map.put("pass", userpassword);
                XUtils.Get(url, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            String code = jsonObject.optString("code");
                            if (code.equals("4200")) {
                                Gson gson = new Gson();
                                bean = gson.fromJson(result, LoginBean.class);
                                sp.putStringValue(LoginPage2.this, "username", username);
                                sp.putStringValue(LoginPage2.this, "userpassword", userpassword.toString());
                                ApplicationUtils.getInstance().loginBean = bean;
                                Intent intent = new Intent();
                                intent.setClass(LoginPage2.this, MainActivity.class);
                                startActivity(intent);
                                overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        LogUtil.e(ex.getMessage() + "联网请求失败");
                    }
                });

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qq:
                UMShareAPI.get(this).getPlatformInfo(this, SHARE_MEDIA.QQ, qqUmAuthListener);
                break;
            case R.id.weixin:
                UMShareAPI.get(this).getPlatformInfo(this, SHARE_MEDIA.WEIXIN, weiXinUmAuthListener);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 微信授权
     */
    private UMAuthListener weiXinUmAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            Toast.makeText(LoginPage2.this, "微信正在授权，请稍后...", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onComplete(SHARE_MEDIA share_media, int i,
                               final Map<String, String> map) {
            Log.e("666666", map + "---" + map.get("openid"));
            map.put("tid", "1");
            String url = AppUrl.URL + AppUrl.LoginURL;
            XUtils.Get(url, map, new MyCallBack<String>() {
                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String message = jsonObject.optString("message");
                    String results = jsonObject.optString("result");
                    if (results.equals("true")) {
                        Gson gson = new Gson();
                        bean = gson.fromJson(result, LoginBean.class);
                        sp.putStringValue(LoginPage2.this, "openid",map.get("openid"));
                        ApplicationUtils.getInstance().loginBean = bean;
                        Intent intent = new Intent();
                        intent.setClass(LoginPage2.this, MainActivity.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                        Toast.makeText(LoginPage2.this, "授权成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(LoginPage2.this, message + "登录失败", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    super.onError(ex, isOnCallback);
                    Toast.makeText(LoginPage2.this, ex.getMessage()+ "登录失败", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFinished() {
                    super.onFinished();
                }
            });
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

            Toast.makeText(LoginPage2.this, "授权失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {
            Toast.makeText(LoginPage2.this, "授权取消", Toast.LENGTH_SHORT).show();
        }
    };


    /**
     * QQ授权
     */
    protected UMAuthListener qqUmAuthListener = new UMAuthListener() {

        @Override
        public void onStart(SHARE_MEDIA share_media) {
//            startProgressDialog("");
            Toast.makeText(LoginPage2.this, "QQ正在授权，请稍后...", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onComplete(SHARE_MEDIA share_media, int i,
                               Map<String, String> map) {
            Log.e("123", "123");
            Log.e("WhatMessahe", map.toString() + "---" + map.get("uid"));
            Toast.makeText(LoginPage2.this, "授权成功", Toast.LENGTH_SHORT).show();
//            if (map != null) {
//                String accessToken = map.get("accessToken");
//                String openid = map.get("openid");
//                HeadImage = map.get("iconurl");
//                Name = map.get("name");
//                GoTest("qq",accessToken,openid);
//            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
            Log.e("123", "234");
            Toast.makeText(LoginPage2.this, "授权失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {
            Log.e("123", "345");
            Toast.makeText(LoginPage2.this, "授权取消", Toast.LENGTH_SHORT).show();
        }
    };
}
